<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库区名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入库区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库区类型" prop="areaType">
        <el-select v-model="queryParams.areaType" placeholder="请选择库区类型" clearable>
          <el-option
            v-for="dict in dict.type.wms_warehouse_area_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库区状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择库区状态" clearable>
          <el-option
            v-for="dict in dict.type.wms_warehouse_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:wmsWarehouseArea:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:wmsWarehouseArea:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:wmsWarehouseArea:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:wmsWarehouseArea:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wmsWarehouseAreaList" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="库区名称" align="center" prop="areaName" />
      <el-table-column label="库区类型" align="center" prop="areaType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_warehouse_area_type" :value="scope.row.areaType"/>
        </template>
      </el-table-column>
      <el-table-column label="温度范围" align="center" prop="temperatureRange" />
      <el-table-column label="湿度范围" align="center" prop="humidityRange" />
      <el-table-column label="库区负责人" align="center" prop="manager" />
      <el-table-column label="库区状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_warehouse_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="库区描述" align="center" prop="description" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:wmsWarehouseArea:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:wmsWarehouseArea:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库区域信息对话框 -->
    <el-drawer
      :title=title
      :visible.sync="open"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
      size="48%"
    >
      <div class="demo-drawer__content">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属仓库" prop="warehouseId">
              <el-select v-model="form.warehouseId" placeholder="请选择所属仓库" style="width: 65%" >
                <el-option v-for="item in this.warehouseSelect" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库区名称" prop="areaName">
              <el-input v-model="form.areaName" placeholder="请输入库区名称" style="width: 50%"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="库区类型" prop="areaType">
          <el-select v-model="form.areaType" placeholder="请选择库区类型">
            <el-option
              v-for="dict in dict.type.wms_warehouse_area_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-row>
          <el-col :span="12">
            <el-form-item label="温度范围" prop="temperatureRange">
              <el-input v-model="form.temperatureRange" placeholder="请输入温度范围" style="width: 50%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="湿度范围" prop="humidityRange">
              <el-input v-model="form.humidityRange" placeholder="请输入湿度范围" style="width: 50%"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="库区负责人" prop="manager" >
              <el-input v-model="form.manager" placeholder="请输入库区负责人" style="width: 50%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库区状态" prop="status"  >
              <el-select v-model="form.status" placeholder="请选择库区状态" style="width: 50%">
                <el-option
                  v-for="dict in dict.type.wms_warehouse_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>




        <el-form-item label="库区描述" prop="description" >
          <el-input v-model="form.description" placeholder="请输入库区描述" type="textarea" style="width: 60%"/>
        </el-form-item>
      </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: center;">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>

    </el-drawer>
  </div>
</template>

<script>
import { listWmsWarehouseArea, getWmsWarehouseArea, delWmsWarehouseArea, addWmsWarehouseArea, updateWmsWarehouseArea } from "@/api/system/wmsWarehouseArea"
import {getWarehouseSelect} from "@/api/system/selectVo"

export default {
  name: "WmsWarehouseArea",
  dicts: ['wms_warehouse_status', 'wms_warehouse_area_type'],
  data() {
    return {
      warehouseSelect:[],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库区域信息表格数据
      wmsWarehouseAreaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 库区描述时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: null,
        areaName: null,
        areaType: null,
        temperatureRange: null,
        humidityRange: null,
        manager: null,
        status: null,
        description: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseId: [
          { required: true, message: "所属仓库ID不能为空", trigger: "blur" }
        ],
        areaName: [
          { required: true, message: "库区名称不能为空", trigger: "blur" }
        ],
        areaType: [
          { required: true, message: "库区类型:1-普通区,2-冷藏区,3-危险品区,4-散货区,5-其他不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "库区状态:1-活跃,2-停用,3-维护中不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询仓库区域信息列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0]
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1]
      }
      listWmsWarehouseArea(this.queryParams).then(response => {
        this.wmsWarehouseAreaList = response.rows
        this.total = response.total
        this.loading = false
      })

      getWarehouseSelect().then(response => {
        this.warehouseSelect = response.data;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        areaId: null,
        warehouseId: null,
        areaName: null,
        areaType: null,
        temperatureRange: null,
        humidityRange: null,
        manager: null,
        status: null,
        description: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.areaId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加仓库区域信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const areaId = row.areaId || this.ids
      getWmsWarehouseArea(areaId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改仓库区域信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.areaId != null) {
            updateWmsWarehouseArea(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addWmsWarehouseArea(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const areaIds = row.areaId || this.ids
      this.$modal.confirm('是否确认删除仓库区域信息编号为"' + areaIds + '"的数据项？').then(function() {
        return delWmsWarehouseArea(areaIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wmsWarehouseArea/export', {
        ...this.queryParams
      }, `wmsWarehouseArea_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
